package com.example.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.common.Result;
import com.example.common.ResultCode;
import com.example.entity.Guoyuanxinxi;
import com.example.exception.CustomException;
import com.example.service.GuoyuanxinxiService;
import com.example.utils.MapWrapperUtils;
import com.example.utils.jwt.JwtUtil;
import com.example.vo.GuoyuanxinxiVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

import cn.hutool.poi.excel.ExcelReader;
import org.springframework.web.multipart.MultipartFile;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/guoyuanxinxi")
public class GuoyuanxinxiController {

    @Resource
    private GuoyuanxinxiService guoyuanxinxiService;

    @PostMapping
    public Result<Guoyuanxinxi> add(@RequestBody GuoyuanxinxiVo guoyuanxinxi) {
		//yoxuxiaxnci
        guoyuanxinxiService.add(guoyuanxinxi);
        return Result.success(guoyuanxinxi);
    }
	
	
    @GetMapping("/getExcel")
    public void getExcel(HttpServletResponse response) throws IOException {
        // 1. 生成excel
        Map<String, Object> row = new LinkedHashMap<>();
        row.put("bianhao","编号");        row.put("mingcheng","名称");        row.put("mianji","面积");        row.put("zhongzhiwu","种植物");        row.put("caizhaizhouqi","采摘周期");        row.put("danjia","单价");        row.put("dizhi","地址");        

        row.put("addtime","添加时间");
        List<Map<String, Object>> list = CollUtil.newArrayList(row);
        List<Map<String, Object>> daochuexcellist = guoyuanxinxiService.daochuexcel();
        for (Map<String, Object> map : daochuexcellist) {
            list.add(map);
        }
        // 2. 写excel
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(list, true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=chaoba.xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(System.out);
    }
	
	

    @PostMapping("/deleteList")
    public Result<Guoyuanxinxi> deleteList(@RequestBody GuoyuanxinxiVo guoyuanxinxi) {
        guoyuanxinxiService.deleteList(guoyuanxinxi.getList());
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        guoyuanxinxiService.delete(id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody GuoyuanxinxiVo guoyuanxinxi) {
        guoyuanxinxiService.update(guoyuanxinxi);
        return Result.success();
    }
	
	@PutMapping("/updateByzhujian")
    public Result updateByzhujian(@RequestBody Guoyuanxinxi guoyuanxinxi) {
        guoyuanxinxiService.updateByzhujian(guoyuanxinxi);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Guoyuanxinxi> detail(@PathVariable Integer id) {
        Guoyuanxinxi guoyuanxinxi = guoyuanxinxiService.findById(id);
        return Result.success(guoyuanxinxi);
    }
	
	@GetMapping("/getByzhujian/{zhujian}")
    public Result<Guoyuanxinxi> getByzhujian(@PathVariable String zhujian) {
        Guoyuanxinxi guoyuanxinxi = guoyuanxinxiService.getByzhujian(zhujian);
        return Result.success(guoyuanxinxi);
    }

    @GetMapping
    public Result<List<Guoyuanxinxi>> all() {
        return Result.success(guoyuanxinxiService.list());
    }

    @PostMapping("/page")
    public Result<GuoyuanxinxiVo> page(@RequestBody GuoyuanxinxiVo guoyuanxinxiVo) {
        return Result.success(guoyuanxinxiService.findPage(guoyuanxinxiVo));
    }
	
	@PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<Guoyuanxinxi> guoyuanxinxis = reader.readAll(Guoyuanxinxi.class);
		
        guoyuanxinxiService.saveBatch(guoyuanxinxis);
        return Result.success();
    }
	
	//youupdt2login
}
